package be.phl.layoutAdapter;

import java.text.SimpleDateFormat;
import java.util.List;

import be.phl.model.DBAdapterScreening;
import be.phl.obstacleapp.R;
import be.phl.obstacleapp.ScreeningSummaryActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable.Callback;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class ScreeningRowAdapter extends ArrayAdapter<ScreeningRow> {

	private final Context context;
	private final List<ScreeningRow> screenings;
	public Callbacks onItemClickCallbacks;

	/**
	 * callback to return the item clicked
	 * 
	 * @author krisdelanghe
	 * 
	 */
	public interface Callbacks {
		public void onItemClicked(ScreeningRow selected);
	}

	public ScreeningRowAdapter(Context context, List<ScreeningRow> screenings) {
		super(context, R.layout.screening_row, screenings);
		// TODO Auto-generated constructor stub

		this.context = context;
		this.screenings = screenings;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.screening_row, parent, false);

		TextView txtScreening = (TextView) rowView.findViewById(R.id.screening);
		TextView txtPatient = (TextView) rowView.findViewById(R.id.patient);
		TextView txtBuilding = (TextView) rowView.findViewById(R.id.building);
		CheckBox isFinished = (CheckBox) rowView.findViewById(R.id.isFinished);

		// get the screening from at this position
		final ScreeningRow screening = screenings.get(position);
		rowView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onItemClickCallbacks = (Callbacks) context;
				onItemClickCallbacks.onItemClicked(screening);
			}

		});

		txtScreening
				.setText("Datum: "
						+ new SimpleDateFormat("dd/MM/yyyy").format(screening
								.getDate()));
		txtPatient.setText(screening.getFirstName() + " "
				+ screening.getLastName());
		txtBuilding.setText(screening.getAddress());

		isFinished.setChecked(screening.isFinished());
		isFinished.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {

				// update the screening when finished
				DBAdapterScreening db = new DBAdapterScreening(context);
				db.open();
				db.updateScreeningFinished(screening.getScreeningId(),
						isChecked ? "T" : "F");
				db.close();

			}

		});

		// set longclicklistener
		rowView.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(context)
				.setTitle(
						"Wilt u de screening verwijderen?")
				.setPositiveButton(R.string.ok,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int id) {
								// delete screening
								DBAdapterScreening db = new DBAdapterScreening(context);
								db.open();
								((DBAdapterScreening) db).deleteScreening(screening.getScreeningId());
								db.close();
								
								screenings.remove(position);
								ScreeningRowAdapter.this.notifyDataSetChanged();
							}
						})
				.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int id) {
								// User cancelled the dialog

							}
						}).show();
				
				return false;
			}
		});

		return rowView;
	}
}
